﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Globalization;
using System.Resources;
using System.Threading;
using System.Data;
using System.Web.UI.HtmlControls;

namespace Phoenix
{
    public partial class Tracking : System.Web.UI.Page
    {
        ResourceManager rm;
        CultureInfo ci;
        Phoenix.AppCode.Entities.TrackingE track = new Phoenix.AppCode.Entities.TrackingE();
        Phoenix.AppCode.Entities.Article home = new Phoenix.AppCode.Entities.Article();
        protected void Page_Load(object sender, EventArgs e)
        {
            HttpCookie fenix_lang = Request.Cookies["Culture"];
            if (fenix_lang == null)
            {
                fenix_lang = new HttpCookie("Culture", "en-US");
                fenix_lang.Expires = DateTime.Now.AddHours(24);
                Response.Cookies.Add(fenix_lang);
                Thread.CurrentThread.CurrentCulture = new CultureInfo("en-US");
                rm = new ResourceManager("Resources.Language", System.Reflection.Assembly.Load("App_GlobalResources"));
                ci = Thread.CurrentThread.CurrentCulture;
                Load_Tracking(ci);
                LoadDB_US();
            }
            else
            {
                if (fenix_lang.Value == "vi-VN")
                {
                    Thread.CurrentThread.CurrentCulture = new CultureInfo("vi-VN");
                    rm = new ResourceManager("Resources.Language", System.Reflection.Assembly.Load("App_GlobalResources"));
                    ci = Thread.CurrentThread.CurrentCulture;
                    Load_Tracking(ci);
                    LoadDB_VN();
                }
                else
                {
                    Thread.CurrentThread.CurrentCulture = new CultureInfo("en-US");
                    rm = new ResourceManager("Resources.Language", System.Reflection.Assembly.Load("App_GlobalResources"));
                    ci = Thread.CurrentThread.CurrentCulture;
                    Load_Tracking(ci);
                    LoadDB_US();

                }
            }
        }
        public void Load_Tracking(CultureInfo ci)
        {
            //top menu
            string track_title = rm.GetString("track_title", ci);
            string track_date = rm.GetString("track_date", ci);
            string track_comt = rm.GetString("track_comt", ci);
            string track_event = rm.GetString("track_event", ci);
            lb_title.Text = track_title;
            lb_event.Text = track_event;
            lb_date.Text = track_date;
            lb_status.Text = track_comt;
        }

        private void LoadDB_VN()
        {
            int id = 1;
            string lang = "vi-VN";
            home.ID_ArticleCategory = id;
            home.Language = lang;
            DataTable dt = Phoenix.AppCode.Bussiness.ArticleService.ArticleLoadService(home);
            if (dt.Rows.Count > 0)
            {
                DataRow dr = dt.Rows[0];

                string dtitle = dr["TitleName"].ToString();
                string htitle = dr["TitleHead"].ToString();
                string des = dr["DescriptionHead"].ToString();
                string key = dr["KeywordHead"].ToString();
                HtmlHead headTag = (HtmlHead)this.Header;
                if (htitle.Length < 2)
                {
                    headTag.Title = dtitle;
                }
                else
                {
                    headTag.Title = htitle;
                }
                HtmlMeta PagemetaTag = new HtmlMeta();
                PagemetaTag.Name = "description";
                PagemetaTag.Content = des;
                headTag.Controls.Add(PagemetaTag);

                PagemetaTag = new HtmlMeta();
                PagemetaTag.Name = "keywords";
                PagemetaTag.Content = key;
                headTag.Controls.Add(PagemetaTag);
            }
        }
        private void LoadDB_US()
        {
            int id = 1;
            string lang = "en-US";
            home.ID_ArticleCategory = id;
            home.Language = lang;
            DataTable dt = Phoenix.AppCode.Bussiness.ArticleService.ArticleLoadService(home);
            if (dt.Rows.Count > 0)
            {
                DataRow dr = dt.Rows[0];

                string dtitle = dr["TitleName"].ToString();
                string htitle = dr["TitleHead"].ToString();
                string des = dr["DescriptionHead"].ToString();
                string key = dr["KeywordHead"].ToString();
                HtmlHead headTag = (HtmlHead)this.Header;
                if (htitle.Length < 2)
                {
                    headTag.Title = dtitle;
                }
                else
                {
                    headTag.Title = htitle;
                }
                HtmlMeta PagemetaTag = new HtmlMeta();
                PagemetaTag.Name = "description";
                PagemetaTag.Content = des;
                headTag.Controls.Add(PagemetaTag);

                PagemetaTag = new HtmlMeta();
                PagemetaTag.Name = "keywords";
                PagemetaTag.Content = key;
                headTag.Controls.Add(PagemetaTag);
            }
        }
        protected void bt_submit_Click(object sender, EventArgs e)
        {
            string srt = "";
            HttpCookie fenix_lang = Request.Cookies["Culture"];
            if (fenix_lang == null)
            {
                string idtrack = txt_track.Text;
                string lang = "en-US";
                track.NumberTrackingE = idtrack;
                track.Language = lang;
                DataTable dt = Phoenix.AppCode.Bussiness.TrackingEService.GetTrackByNumber(track);
                if (dt.Rows.Count > 0)
                {
                    for (int i = 0; i < dt.Rows.Count; i++)
                    {
                        srt += "<tr><td>" + dt.Rows[i]["TrackEvent"] + "</td><td>" + String.Format("{0:d}", dt.Rows[i]["Date"]) + "</td><td>" + dt.Rows[i]["State"] + "</td></tr>";
                    }
                    lb_error.Text = "";
                }
                else
                {
                    string track_error = rm.GetString("track_error", ci);
                    lb_error.Text = track_error;
                }
                lb_content.Text = srt;
            }
            else
            {
                if (fenix_lang.Value == "vi-VN")
                {
                    string idtrack = txt_track.Text;
                    string lang = "vi-VN";
                    track.NumberTrackingE = idtrack;
                    track.Language = lang;
                    DataTable dt = Phoenix.AppCode.Bussiness.TrackingEService.GetTrackByNumber(track);
                    if (dt.Rows.Count > 0)
                    {
                        for (int i = 0; i < dt.Rows.Count; i++)
                        {
                            srt += "<tr><td>" + dt.Rows[i]["TrackEvent"] + "</td><td>" + String.Format("{0:d}", dt.Rows[i]["Date"]) + "</td><td>" + dt.Rows[i]["State"] + "</td></tr>";
                        }
                        lb_error.Text = "";
                    }
                    else
                    {
                        string track_error = rm.GetString("track_error", ci);
                        lb_error.Text = track_error;
                    }
                    lb_content.Text = srt;
                }
                else
                {
                    string idtrack = txt_track.Text;
                    string lang = "en-US";
                    track.NumberTrackingE = idtrack;
                    track.Language = lang;
                    DataTable dt = Phoenix.AppCode.Bussiness.TrackingEService.GetTrackByNumber(track);
                    if (dt.Rows.Count > 0)
                    {
                        for (int i = 0; i < dt.Rows.Count; i++)
                        {
                            srt += "<tr><td>" + dt.Rows[i]["TrackEvent"] + "</td><td>" + String.Format("{0:d}", dt.Rows[i]["Date"]) + "</td><td>" + dt.Rows[i]["State"] + "</td></tr>";
                        }
                        lb_error.Text = "";
                    }
                    else
                    {
                        string track_error = rm.GetString("track_error", ci);
                        lb_error.Text = track_error;
                    }
                    lb_content.Text = srt;
                }
            }

        }
    }
}